package com.shamoa.springbootredis.factory;

import org.springframework.beans.factory.BeanCreationException;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.scripting.support.ResourceScriptSource;

public class RedisScriptFactory {

    private static RedisScriptFactory redisScriptFactoryInstance = new RedisScriptFactory();

    private RedisScriptFactory() {

    }

    public static RedisScriptFactory getInstance() {
        return redisScriptFactoryInstance;
    }

    /**
     * 生成RedisScript
     */
    public <T> DefaultRedisScript<T> buildRedisScript(String luaPath, Class<T> classType) {
        DefaultRedisScript<T> redisScript = new DefaultRedisScript<>();
        try {
            redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("luaScript/" + luaPath)));
        } catch (Exception ex) {
            return redisScript;
        }
        redisScript.setResultType(classType);
        return redisScript;
    }

}
